*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		3b_tables_ancova_consumption.do
*-------------------------------------------------------------------------------
* Description		Creates consumption cutbacks ANCOVA table 
* Outputs		Table A9 
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Tables
*2.1)	Consumption Cutbacks for people who had an economic shock and AES
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================

clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data 
global results "$path/results"		
global inputPath "$path/datasets"
cd "$results"									// Change to directory to store results 
set more off
set trace off

*Loading processed data
	use $inputPath/impactDatabase_wide.dta, clear		

*Merge the asesor data here. 															
merge 1:1 idBaseSurvey using $inputPath/asesor2008.dta
drop if _merge == 2
drop _merge 

*===============================================================================
* Section 2 - Tables 
*===============================================================================
* Section 2.1 - Table on consumption cutback with economic shock
*-------------------------------------------------------------------------------
* 2.1.1.Calculates table content -----------------------------------------------
*-------------------------------------------------------------------------------
		
	eststo clear
	local i = 1
	
	foreach y in mealsCutback meatCutback medicinesCutback 			///
	schoolSuppliesCutback clothingCutback schoolSnacksCutback 		///
	transportCutback eatingOutCutback { 

		gen nonmiss = 1 if `y'2009 !=. & `y'2008 !=. // Marking subjects nonmissing in both periods
		reghdfe `y'2009 i.accountAccess `y'2008  if nonmiss ==1        &     ///
                economicShock2009 == 1 , abs(asesor2008) vce(cluster groupId) 

		//contol mean
		sum `y'2009 if accountAccess == 0 & economicShock2009 == 1 	// control's mean outcome
		if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}

		//AES calculation, step 1: normalizes variables
		sum `y'2009 if accountAccess==0 //computes sd
		gen aes_cc`i'2009 = `y'2009 / r(sd) //normalizes outcome variable
		
		sum `y'2008 if accountAccess==0 //computes sd
		gen aes_cc`i'2008 = `y'2008 / r(sd) //normalizes outcome variable

		estadd local ind_fixeff = "No"
		estadd local str_fixeff = "Yes"
		drop nonmiss
			
		eststo
		local i = `i' + 1 //updates counter	
			
		
	}
	
	//AES calculation, step 2: reshapes the data to implement regression
	// we keep only variables required
	keep aes_cc* idBaseSurvey economicShock2009 groupId accountAccess asesor2008
	reshape long aes_cc, i(idBaseSurvey asesor2008) j(out)
	
	tostring(out), generate(year)
	replace year = substr(year, 2, 4)
	destring(year), replace //creates year variable 
	
	tostring(out), replace
	replace(out) = substr(out, 1, 1)
	destring(out), replace //creates out variable
	
	reshape wide aes_cc , i(idBaseSurvey out) j(year)
	
	//AES: Implements regression
	reghdfe aes_cc2009 c.accountAccess#i.out ///
	i.out aes_cc2008 if economicShock2009==1, abs(asesor2008) vce(cluster groupId)
	
	//AES: computes AES
	lincom((1.out#c.accountAccess + 2.out#c.accountAccess + 	///
		3.out#c.accountAccess + 4.out#c.accountAccess + 	///
		5.out#c.accountAccess + 6.out#c.accountAccess + 	///
		7.out#c.accountAccess + 8.out#c.accountAccess) /8)	

	local aes_se = trim("`: display %12.3fc r(se)'")
	local aes_p = 2*ttail(`r(df)',abs(`r(estimate)' / `r(se)'))
	// stores stars for coefficient
	if (`aes_p' <= 0.1 & `aes_p' > 0.05 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'*")
	}
	else if (`aes_p' <= 0.05 & `aes_p' > 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'**")
	}
	else if (`aes_p' <= 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'***")
	}
	else {
		local aes_b = trim("`: display %12.3fc r(estimate)'")
	}
	
	
*-------------------------------------------------------------------------------
* 2.1.2. Writes tables
*-------------------------------------------------------------------------------
	esttab est* using tab_consumption_cutback_shock_ancova.tex, replace 							///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber 						///
	stats( control_mean N ind_fixeff str_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 						///
	label("Control mean" "\hline Observations" "Individual FE" "Stratum FE")) mlabels(,none)				///
	collabels(, none) eqlabels(, none) 											/// 
	keep(1.accountAccess )  												///
	varlabels(1.accountAccess "Account") 											///
	starlevels(* 0.1 ** 0.05 *** 0.01) 											///
	prehead(\begin{tabular}{p{3.75 cm} l*{@M}{c}} \hline \hline 								///
	& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ 									///
	& Meals & Meat & Medicines & School & Clothing & School & Public & Eating \\ 						///
	& & & & supplies & & snacks & transport & out  \\) 									///
	posthead(\hline) prefoot( ) 												///
	postfoot(\hline \multicolumn{@span}{c}{\textbf{AES:} `aes_b' (`aes_se')} \\ 						///
	\hline \hline \end{tabular}) 	
